package com.example.weather;

import android.app.AlertDialog;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.weather.bean.WeaBean;
import com.example.weather.bean.WeatherBean;
import com.example.weather.db.DBManager;
import com.google.gson.Gson;

import java.net.CookieHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CityWeatherFragment#} factory method to
 * create an instance of this fragment.
 */
public class CityWeatherFragment extends BaseFragment implements View.OnClickListener{
    private static final String TAG ="CityWeatherFragment";
    TextView tempTv,cityTv,conditionTv,windTv,tempRangeTv,dateTv,clothIndexTv,sportIndexTv,raysIndexTv,sunrise,sunset;
    ImageView dayIv;
    LinearLayout futureLayout;
    String url1 = "http://api.tianapi.com/txapi/tianqi/index?key=5fcc28098b884f606428663b7df174ae&city=";
    String city;
    String url;
    String image;
    WeaBean weaBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_city_weather, container, false);
        initView(view);
        //接收数据
        Bundle bundle = getArguments();
        city = bundle.getString("city");
        url=url1+city;
        Log.i(TAG, "onCreateView: 访问网址:"+url);
        loadData(url);
        return view;
        //通过activity传值获取当前fragment加载的是哪个地方的天气预报
    }
    @Override
    public void onSuccess(String result) {
//        解析并展示数据
        parseShowData(result);
        Log.i(TAG, "onSuccess: 解析成功");
        //更新数据
        int i=DBManager.updateInfoByCity(city,result);
        if(i<=0){
            //没有这个城市，更新失败
            Log.i(TAG, "onSuccess: 更新数据失败，没有这个城市");
            DBManager.addCityInfo(city,result);
        }
    }
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
//        数据库当中查找上一次信息显示在Fragment当中
        String s = DBManager.queryInfoByCity(city);
        if (!TextUtils.isEmpty(s)) {
            parseShowData(s);
        }

    }


    private void parseShowData(String result) {
        //使用gson解析数据
        Map<String, Integer> weaImMap = new HashMap<String, Integer>();
        weaImMap.put("qing.png",R.drawable.qing);
        weaImMap.put("lei.png", R.drawable.lei);
        weaImMap.put("yu.png", R.drawable.yu);
        weaImMap.put("yin.png", R.drawable.yin);
        weaImMap.put("shachen.png", R.drawable.shachen);
        weaImMap.put("wu.png", R.drawable.wu);
        weaImMap.put("xue.png", R.drawable.xue);
        weaImMap.put("yun.png", R.drawable.yun);
        Log.i(TAG, "parseShowData: 解析数据");
        weaBean =new Gson().fromJson(result,WeaBean.class);
        WeaBean.NewslistDTO newslistDTO=weaBean.getNewslist().get(0);
        Log.i(TAG, "parseShowData: newslistDT0:"+newslistDTO);
        //获取信息
        cityTv.setText(newslistDTO.getArea());
        Log.i(TAG, "parseShowData: 地区："+newslistDTO.getArea());
        dateTv.setText(newslistDTO.getDate());
        Log.i(TAG, "parseShowData: 日期："+newslistDTO.getDate());
        tempTv.setText(newslistDTO.getReal());
        Log.i(TAG, "parseShowData: 实时温度："+newslistDTO.getReal());
        conditionTv.setText(newslistDTO.getWeather());
        Log.i(TAG, "parseShowData: 天气状况："+newslistDTO.getWeather());
        tempRangeTv.setText(newslistDTO.getLowest()+"~"+newslistDTO.getHighest());
        windTv.setText(newslistDTO.getWind()+newslistDTO.getWindsc());
        sunrise.setText("日出时间："+newslistDTO.getSunrise());
        sunset.setText("日落时间："+newslistDTO.getSunset());
        image= newslistDTO.getWeatherimg();
        dayIv.setImageResource(weaImMap.get(image));
        //dayIv.setImageResource(image);
        //获取未来三天


    }


    private void initView(View view) {
        //初始化控件
        tempTv = view.findViewById(R.id.frag_tv_current);
        cityTv = view.findViewById(R.id.frag_tv_city);
        conditionTv = view.findViewById(R.id.frag_tv_condition);
        windTv = view.findViewById(R.id.frag_tv_wind);
        tempRangeTv = view.findViewById(R.id.frag_tv_temprange);
        dateTv = view.findViewById(R.id.frag_tv_date);
        clothIndexTv = view.findViewById(R.id.frag_index_tv_dress);
        dayIv = view.findViewById(R.id.frag_iv_today);
        futureLayout = view.findViewById(R.id.frag_center_layout);
        sunrise=view.findViewById(R.id.frag_tv_sunrise);
        sunset=view.findViewById(R.id.frag_tv_sunset);
        //设置监听
        clothIndexTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        switch(v.getId()){
            case R.id.frag_index_tv_dress:
                builder.setTitle("穿衣推荐");
                WeaBean.NewslistDTO newslistDTO=weaBean.getNewslist().get(0);
                String msg1=newslistDTO.getTips();
                builder.setMessage(msg1);
                builder.setPositiveButton("确定",null);
                break;

        }
        builder.create().show();
    }
}